package com.prueba.client.configuracion;



import com.prueba.client.datos.VistasData;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.IPickTreeItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;

public class TablaVistasYCostos extends Window{
	
	static public TablaVistasYCostos getInstance(){
		if (singleton==null){
			singleton=new TablaVistasYCostos();
		}
		return singleton;
	}
	
	static private TablaVistasYCostos singleton=null;
	

	
	private TablaVistasYCostos(){
		super();
        this.setAutoSize(true);  
        this.setTitle("Documento"); 
		this.setShowMaximizeButton(false);
		this.setShowMinimizeButton(false);
		this.setShowCloseButton(false);
		this.setCanDragReposition(false);
		
		//grilla del documento
		TreeGrid document=DocumentTreeGrid.getInstance();
		
		//boton agregar seccion:
		IButton addSection = new IButton("Agregar seccion");
        addSection.setAutoFit(true); 
        addSection.addClickHandler(new ClickHandler() {  
            public void onClick(ClickEvent event) {  
                NewSectionWindow.getInstance().show();
            } });
        addSection.setLayoutAlign(Alignment.RIGHT); 
        
        
        //boton eliminar seccion:
		IButton deleteSection = new IButton("Eliminar seccion");
        addSection.setAutoFit(true); 
        addSection.addClickHandler(new ClickHandler() {  
            public void onClick(ClickEvent event) {  
                //eliminar la seccion seleccionada
            } });
        deleteSection.setLayoutAlign(Alignment.RIGHT); 
        
        //formulario costo de iteracion:
        DynamicForm form=new DynamicForm();
        TextItem costoIteracion = new TextItem("costoIteracion","Costo iteracion");
        costoIteracion.setType("INTEGER");
        form.setFields(costoIteracion);
        
        //agregando componentes a la ventana:
        VLayout layout=new VLayout();
        	layout.addMember(document);
				//document.setWidth("100%");   
				//document.setHeight("100%");         	
        	HLayout hlayout=new HLayout();
        		hlayout.addMember(deleteSection);
        		hlayout.addMember(addSection);
        		hlayout.addMember(form);
        	layout.addMember(hlayout); 
        this.addItem(layout);

	}
	
	
	static class DocumentTreeGrid extends TreeGrid{
		static DocumentTreeGrid getInstance(){
			if (singleton==null){
				singleton=new DocumentTreeGrid();
			}
			return singleton;
		}
		static private DocumentTreeGrid singleton=null;
		
		private DocumentTreeGrid(){

			 //Parametros de la grilla  

		     
		     this.setNodeIcon("vista.png");   
		     this.setFolderIcon("vista.png");
		     
		     //this.setCanFreezeFields(true);   
		     //this.setCanReparentNodes(true);           
		     
		     this.setDataSource(VistasData.getInstance());
		     this.setAutoFetchData(true);
		     this.setLoadDataOnDemand(false);     
		     this.setCanEdit(true);
		     this.setWidth(400);  
		     this.setHeight(300); 
		    
		     //Campos de la grilla
		     TreeGridField nombre = new TreeGridField("vista", "Seccion",150);   
		     nombre.setFrozen(true);   
		     TreeGridField complejidad = new TreeGridField("complejidad","Complejidad", 150);   
		     complejidad.setValueMap("baja","media","alta");
		     TreeGridField elementos = new TreeGridField("elementos","Elementos", 150);   
		     TreeGridField relaciones = new TreeGridField("relaciones","Relaciones", 150);   
		     //TreeGridField padre = new TreeGridField("padre","Seccion padre", 150); NO SE TENDRIA QUE MOSTRAR   
		     this.setFields(nombre,complejidad,elementos,relaciones);
		     this.setOverflow(Overflow.AUTO);
		}

	}	

	static class NewSectionWindow extends Window{
		static NewSectionWindow getInstance(){
			if (singleton==null){
				singleton=new NewSectionWindow();
			}
			return singleton;
		}
		static private NewSectionWindow singleton=null;
		static private DynamicForm form=null;
		private NewSectionWindow(){
	        //parametros de la ventana:
			this.setAutoSize(true);  
	        this.setTitle("Agregar nueva seccion");  
	        //this.setWidth(width);  
	        //this.setHeight(height);  
	        this.setLeft(400);  
	        this.setCanDragReposition(true);  
	        //this.setCanDragResize(true);  
	        
	        //elementos del formulario:
	        form = new DynamicForm(); 
	        form.setDataSource(VistasData.getInstance());
	        

	        
	        TextItem nameItem = new TextItem("vista","Seccion"); 
	        SelectItem complexityItem = new SelectItem("complejidad","Complejidad");
	        complexityItem.setValueMap("baja","media","alta"); 
	        complexityItem.setValue("baja");//puede no funcar
	        TextItem elementsItem = new TextItem("elementos","Cant. elementos"); 
	        elementsItem.setValue("1");
	        TextItem relacionesItem = new TextItem("relaciones","Cant. relaciones"); 
	        elementsItem.setValue("1");

	        //arreglar el search pick tree, es cualquiero, por ahora:
	        IPickTreeItem searchPickTree = new IPickTreeItem("padre","Seccion padre");  
	        searchPickTree.setCanSelectParentItems(true);  
	        searchPickTree.setShowTitle(false);  
	        searchPickTree.setDataSource(VistasData.getInstance());  
	        searchPickTree.setDisplayField("vista");  
	        searchPickTree.setValueField("padre");  
	        form.setFields(
		            nameItem,complexityItem,elementsItem,relacionesItem,searchPickTree);
	        
	        
	        //boton de agregar:
	        IButton ok = new IButton("OK");
            ok.setAutoFit(true); 
            ok.addClickHandler(new ClickHandler() {  
                public void onClick(ClickEvent event) {  
                    if(form.validate()){
                    	VistasData.getInstance().addData(form.getValuesAsRecord());
                    	//porque agrega gente con igual primari key???
                    	NewSectionWindow.getInstance().hide();
                    }
                	/*try{
                    	String name=(String)(form.getField("name").getValue());
                    	if(name==null || name.equals(""))
                    		throw new RuntimeException("nombre de la seccion no definido");
                    	int elements=Integer.valueOf((String)(form.getField("elements").getValue()));
                    	if(elements<=0)
                    		throw new RuntimeException("La cantidad de elementos debe ser mayor que 0");
                    	int costobase=Integer.valueOf((String)(form.getField("basecost").getValue()));
                    	if(costobase<=0)
                    		throw new RuntimeException("El costo base debe ser mayor que 0");
                    	DocumentTreeGrid.getInstance().addData(form.getValuesAsRecord());//NO FUNCA
                    	NewSectionWindow.getInstance().hide();
                    }catch( RuntimeException e){
                    	SC.warn(e.getMessage());
                    }*/
                    
                } });
            ok.setLayoutAlign(Alignment.RIGHT); 
            
            //agregando componentes a la ventana:
            VLayout layout=new VLayout();
            layout.addMember(form); 
            layout.addMember(ok);
            this.addItem(layout);
            
		}
	}
	
}
